var app = getApp();

import api from '../../../utils/api.js'
var app = getApp();
var QQMapWX = require("../../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
import { HTTP } from '../../../utils/http.js'
import utils from '../../../utils/util.js'
import { config } from '../../../utils/config'

let http = new HTTP()

Page({

	data: {
		imgUrl: config.imgUrl,
		winHeight: "",//窗口高度
		currentTab: 0, //预设当前项的值
		scrollLeft: 0, //tab标题的滚动条位置
		viewDesc: false,
		EatCateIndex: 0,
		content_info:'',
		page: 1,
		canRequest: true,
		eatCate: [
			{ name: '益食', id: 1 },
			{ name: '忌食', id: 2 }
		],
		cookList: []
	},
	catchTouchMove: function () {
		return false;
	},
	// 滚动切换标签样式
	switchTab: function (e) {
		if (!this.data.canRequest) {
			return;
		} else {
			this.setData({
				canRequest: false
			})
		}
		let cur = e.detail.current
		this.setData({
			currentTab: cur
		});

		let category = this.data.category
		let id = category[cur].id;
		this.setData({
			id,
			cookList: [],
			page: 1,
			viewDesc: false,
			result: []
		})
		this.updataInfo();

		this.checkCor();
	},
	// 点击标题切换当前页时改变样式
	swichNav: function (e) {
		var cur = e.target.dataset.current;
		if (this.data.currentTaB == cur) { return false; }
		else {
			this.setData({
				currentTab: cur
			})
		}
		let category = this.data.category
		let id = category[cur].id;
		this.setData({
			id,
			cookList: [],
			page: 1,
			viewDesc: false,
			result: []
		})
		this.updataInfo();
		this.checkCor();

	},
	// 判断当前滚动超过一屏时，设置tab标题滚动条。
	checkCor: function () {
		if (this.data.currentTab > 4) {
			this.setData({
				scrollLeft: this.data.currentTab * 56
			})
		} else {
			this.setData({
				scrollLeft: 0
			})
		}
	},
	// checkCor:function(){
	//   this.setData({
	//     scrollLeft: this.data.currentTab * 56
	//   })
	// },
	onLoad: function (options) {
		var that = this;
		let id = options.id;
		let pid = options.pid;

		this.setData({
			id,
			pid
		})


		// 获取顶部导航
		this.getInfo(api.getAllCookCate, "GET", {}, false).then((res) => {
			let category = res.find(item => {
				return item.id == pid
			})
			// 分类下标
			let idIndex;
			category.classes.forEach((item, index) => {
				if (item.id == id) {
					idIndex = index;
				}
			})
			this.setData({
				category: category.classes,
				currentTab: idIndex,
				scrollLeft: 56 * idIndex,
			})
			wx.setNavigationBarTitle({
				title: category.name,
			})
		})

		this.updataInfo();

		//  高度自适应
		wx.getSystemInfo({
			success: function (res) {
				var clientHeight = res.windowHeight,
					clientWidth = res.windowWidth,
					rpxR = 750 / clientWidth;
				var calc = clientHeight * rpxR;
				console.log(calc)
				that.setData({
					winHeight: calc
				});
			}
		});


	},
	updataInfo() {
		let that = this
		this.getInfo(api.getCookgoryInfo, "GET", { id: this.data.id }).then(res => {
			if (res && res.info) {
				res.info = formatRichText(res.info)
			}
			this.setData({
				result: res,
			})
			setTimeout(() => {
				that.setData({
					canRequest: true
				})
			}, 100);

		})

		this.getCookList()

	},

	/*获取菜品一级分类*/
	getInfo(url, method, data, loadding = true) {
		let that = this
		return new Promise((resolve, reject) => {
			if (loadding) {
				wx.showLoading({
					title: '加载中...',
				})
			}

			http.request({
				url: url,
				method: method,
				data: data,
				success: res => {
					if (res.msg && res.error_code) {
						wx.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1500
						});
						return;
					}
					if (res.code == 200) {
						resolve(res.data);
					}
					if (loadding) {
						wx.hideLoading()
					}

				},
				fail: () => { },
				complete: () => {
					wx.hideLoading();
				}


			})
		})
	},
	getCookList() {
		let that = this
		let address = wx.getStorageSync('address');
		let data = {
			lat: address.latitude,
			lng: address.longitude,
			id: this.data.id,
			page: this.data.page
		}
		this.getInfo(api.shopCateCookBooks, "GET", data).then(res => {
			this.setData({
				cookList: that.data.cookList.concat(res.data),
				last_page: res.last_page
			})
		})
	},
	showview() {
		this.setData({
			viewDesc: !this.data.viewDesc
		})
	},
	/*查看推荐菜谱*/
	showVideo(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: "/pages/video/video?id=" + id
		})
	},
	changeCat(e) {
		let index = e.currentTarget.dataset.index;
		this.setData({
			EatCateIndex: index
		})
	},
	scrollToLower: function () {
		console.log('触底刷新');
		if (this.data.page < this.data.last_page) {
			this.setData({
				page: this.data.page + 1
			})
			this.getCookList();
		}
	},

})

function formatRichText(html){
	   let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
       match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
	       match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
	       match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
	       return match;
	   });
	   newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
	       match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
	       return match;
	   });
   newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	   newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:10px 0;"');
	   return newContent;
	 }